//
//  CMDBWorkManager.swift
//  CMDB
//
//  Created by 王磊 on 17/4/10.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBWorkManager: NSObject {
    
    static let getInstance: CMDBWorkManager = CMDBWorkManager()
    
    private override init() {
        super.init()
        
    }
}

extension CMDBWorkManager: CMDBWorkManagerDelegate {}

// MARK: 获取首页作品
extension CMDBWorkManager {
    
    func queryHomeWorks(homeWorksParam: CMDBHomeWorkParam, handler: CMDBHomeWorksHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) {
        CMDBHttpManager.getInstance.request(homeWorksParam.URLString(), param: homeWorksParam.keyValues, success: { (sysTime, data, page) in
            
            let arr = CMDBHomeWorksBean.objectWithKeyValues(data as! NSDictionary) as! CMDBHomeWorksBean
            
            handler(homeWorks: arr)
            
            }, error: error, failure: failure)
    }
    
    func saveHomeWorks(array: Array<CMDBHomeWorksBean>, identifier: String) {
        
    }
    func queryHomeWorks(identifier: String) throws -> Array<CMDBHomeWorksBean> {
        
        
        return []
    }
}
// MARK: 获取作品列表
extension CMDBWorkManager {
    
    func queryWorks(worksParam: CMDBWorkParam,tableView: UITableView, handler: CMDBWorksListHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) {
        
        CMDBHttpManager.getInstance.request(worksParam.URLString(), param: worksParam.keyValues, ownPage: worksParam.otype, tableView: tableView, success: { (sysTime, data, page) in
            
            let arr = CMDBWorksBean.objectArrayWithKeyValuesArray(data as! NSArray) as? Array<CMDBWorksBean>
            
            handler(worksList: arr)
            
            }, error: error, failure: failure)
    }
    func saveWorks(array: Array<CMDBWorksBean>, identifier: String) {
        
        
    }
    
    func queryWorks(identifier: String) throws -> Array<CMDBWorksBean> {
        
        return []
    }
}

// MARK: 获取关联作品
extension CMDBWorkManager {
    
    func queryAssociateMedia(associateParam: CMDBAssociateParam,handler: CMDBAssociateHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) {
        
        CMDBHttpManager.getInstance.request(associateParam.URLString(), param: associateParam.keyValues, success: { (sysTime, data, page) in
            
            let arr: Array<CMDBWorksBean>? = CMDBWorksBean.objectArrayWithKeyValuesArray(data as! NSArray) as? Array<CMDBWorksBean>
            
            handler(associateList: arr)
            
            }, error: error, failure: failure)
    }
}

/*
 do {
 let mutable = NSMutableArray()
 
 mutable.addObject("123")
 
 mutable.addObject("456")
 
 mutable.removeObject("123")
 
 let associateParam: CMDBAssociateToUserParam = CMDBAssociateToUserParam()
 
 if associateParam.associatedIds.count > 0 {
 
 associateParam.associatedIds.removeAll()
 }
 
 _ = mutable.map({
 
 associateParam.associatedIds += [$0 as! String]
 })
 
 try CMDBUserInfoManager.getInstance.associateMediaToUser(associateParam, handler: { (isSuccess) in
 
 
 }, error: { (rescode) in
 
 
 
 }, failure: {
 
 
 })
 } catch {
 
 
 
 
 }
 */
// MARK: 关联作品
extension CMDBWorkManager {
    
    func associateMediaToUser(associateParam: CMDBAssociateToUserParam, handler: CMDBAssociateToUserHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) throws {
        
        guard !associateParam.content.isEmpty else {
            
            printLog(CMDBUserControllerExceptionCodeType.UnSelectedAssociate.rawValue)
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.UnSelectedAssociate)
        }
        
        CMDBHttpManager.getInstance.request(associateParam.URLString(), param: ["content":associateParam.content], success: { (sysTime, data, page) in
            
            handler(isSuccess: true)
            
            }, error: error, failure: failure)
        
    }
}
